package com.xintai.system.controller;

import java.util.List;

import com.xintai.utils.StaticString;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xintai.common.annotation.Log;
import com.xintai.common.core.controller.BaseController;
import com.xintai.common.core.domain.AjaxResult;
import com.xintai.common.enums.BusinessType;
import com.xintai.system.domain.PubWorkshop;
import com.xintai.system.service.IPubWorkshopService;
import com.xintai.common.utils.poi.ExcelUtil;
import com.xintai.common.core.page.TableDataInfo;

/**
 * 生产线Controller
 * 
 * @author xintai
 * @date 2020-12-22
 */
@RestController
@RequestMapping("/system/workshop")
public class PubWorkshopController extends BaseController
{
    @Autowired
    private IPubWorkshopService pubWorkshopService;

//    @PreAuthorize("@ss.hasPermi('system:workshop:selectWorkShopPropertyList')")
//    @GetMapping("/selectWorkShopPropertyList")
//    public TableDataInfo selectWorkShopPropertyList(@PathVariable Long[] workshopIds, @PathVariable Long[] dictCodeIds)
//    {
//        List<PubWorkshop> list = pubWorkshopService.selectWorkShopPropertyList(workshopIds, dictCodeIds);
//        return getDataTable(list);
//    }

    /**
     * 验证指定名称是否重复
     * 如果workshop_id 有数据说明是修改，否则是新增，排除已经del的数据（isdel = 1）
     */
    @PreAuthorize("@ss.hasPermi('system:workshop:verifyPubWorkshop')")
    @GetMapping("/verifyPubWorkshop")
    public boolean verifyPubWorkshop(PubWorkshop pubWorkshop){
        return pubWorkshopService.verifyPubWorkshop(pubWorkshop);
    }

    /**
     * 查询生产线列表
     */
    @PreAuthorize("@ss.hasPermi('system:workshop:list')")
    @GetMapping("/list")
    public TableDataInfo list(PubWorkshop pubWorkshop)
    {
        startPage();
        List<PubWorkshop> list = pubWorkshopService.selectPubWorkshopList(pubWorkshop);
        return getDataTable(list);
    }

    /**
     * 导出生产线列表
     */
    @PreAuthorize("@ss.hasPermi('system:workshop:export')")
    @Log(title = "生产线", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PubWorkshop pubWorkshop)
    {
        List<PubWorkshop> list = pubWorkshopService.selectPubWorkshopList(pubWorkshop);
        ExcelUtil<PubWorkshop> util = new ExcelUtil<PubWorkshop>(PubWorkshop.class);
        return util.exportExcel(list, "workshop");
    }

    /**
     * 获取生产线详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:workshop:query')")
    @GetMapping(value = "/{workshopId}")
    public AjaxResult getInfo(@PathVariable("workshopId") Long workshopId)
    {
        return AjaxResult.success(pubWorkshopService.selectPubWorkshopById(workshopId));
    }

    /**
     * 新增生产线
     */
    @PreAuthorize("@ss.hasPermi('system:workshop:add')")
    @Log(title = "生产线", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PubWorkshop pubWorkshop)
    {
        if (pubWorkshopService.verifyPubWorkshop(pubWorkshop))
            return AjaxResult.error(StaticString.NameRepeat(pubWorkshop.getWorkshopName()));
        return toAjax(pubWorkshopService.insertPubWorkshop(pubWorkshop));
    }

    /**
     * 修改生产线
     */
    @PreAuthorize("@ss.hasPermi('system:workshop:edit')")
    @Log(title = "生产线", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PubWorkshop pubWorkshop)
    {
        if (pubWorkshopService.verifyPubWorkshop(pubWorkshop))
            return AjaxResult.error(StaticString.NameRepeat(pubWorkshop.getWorkshopName()));
        return toAjax(pubWorkshopService.updatePubWorkshop(pubWorkshop));
    }

    /**
     * 删除生产线
     */
    @PreAuthorize("@ss.hasPermi('system:workshop:remove')")
    @Log(title = "生产线", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workshopIds}")
    public AjaxResult remove(@PathVariable Long[] workshopIds)
    {
        return toAjax(pubWorkshopService.deletePubWorkshopByIds(workshopIds));
    }
}
